package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.YanShouDaiDetailsBean;
import com.wnhz.work.yz.bean.YanShouJiChaBean;
import com.wnhz.work.yz.bean.YscpdBean;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShouan44Activity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private YanShouDaiDetailsBean bean = new YanShouDaiDetailsBean();
    private List<YanShouDaiDetailsBean.InfoEntity> info = new ArrayList<>();

    private String type;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_dai_chan_pin);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        EventBus.getDefault().register(this);
        title.setText("套餐列表");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("");
        submit.setTextSize(12);
        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);
        bean = (YanShouDaiDetailsBean) getIntent().getSerializableExtra("bean");
        type = getIntent().getStringExtra("type");
        if (bean == null) {
            MyToast("暂无数据");
            return;
        }

        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.line_color)));
        info = bean.getInfo();
        adapter = new BaseRVAdapter(this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_dai_chanpin;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                //   x.image().bind(holder.getImageView(R.id.img), info.get(position).getImg());
                ImageLoader.getInstance().displayImage(info.get(position).getImg(), holder.getImageView(R.id.img));

                holder.setTextView(R.id.bianhao, "编号：" + info.get(position).getNumber());
                holder.setTextView(R.id.name, info.get(position).getName());
                holder.setTextView(R.id.house, info.get(position).getHouse());
                holder.setTextView(R.id.sku, info.get(position).getInfo());

                if (type.equals("0")) {
                    holder.getTextView(R.id.status).setVisibility(View.GONE);
                } else if (type.equals("0")) {
                    if ("0".equals(info.get(position).getStatus())) {
                        holder.setTextView(R.id.status, "未检");
                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.text102));
                    } else if ("1".equals(info.get(position).getStatus())) {
                        holder.setTextView(R.id.status, "不合格");
                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.red));
                    } else if ("2".equals(info.get(position).getStatus())) {
                        holder.setTextView(R.id.status, "已通过");
                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.main_color));
                    }
                }

                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        getDetails(getIntent().getStringExtra("orderid"));
                    }
                });
            }
        };
        recycler.setAdapter(adapter);
    }

    BaseRVAdapter adapter;

    private void getDetails(String orderid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        showDialog();
        Set keys = map.keySet();

        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e(99 + "----" + key, "" + value + "\n");
            }
        }

        XUtil.Post(Url.DECORATE_ANZHUANGDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtil.e("----安装详情----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouJiChaBean bean = gson.fromJson(result, YanShouJiChaBean.class);
                        startActivity(new Intent(YanShouan44Activity.this, JiChaDetails2Activity.class)
                                .putExtra("type", getIntent().getStringExtra("type"))
                                .putExtra("bean", bean).putExtra("orderid", getIntent().getStringExtra("orderid")));

                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                upload();
                break;
        }
    }

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        StringBuffer sb = new StringBuffer("[");

        for (int i = 0; i < info.size(); i++) {
            sb.append("{\n" +
                    "            \"pid\":\"" + info.get(i).getPid() + "\",\n" +
                    "            \"room_number\":\"" + info.get(i).getHouse().replace("合同号:", "") + "\"\n" +
                    "            }");
            if (i != info.size() - 1) {
                sb.append(",");
            }

        }
        sb.append("]");

        map.put("json_str", sb);
        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        XUtil.Post(Url.DECORATE_ROOMNUMBEROFPACKAGE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        MyToast(info);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }
}
